package com.lnsf.bookstore.system.role.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.constant.Constants;
import com.lnsf.bookstore.common.dto.ResultFailDto;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import com.lnsf.bookstore.common.exception.RException;
import com.lnsf.bookstore.common.session.SessionProvider;
import com.lnsf.bookstore.system.role.service.RolePermissionService;
import com.lnsf.bookstore.system.role.service.RoleService;
import com.lnsf.bookstore.system.role.vo.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private SessionProvider sessionProvider;

    @Autowired
    private RolePermissionService rolePermissionService;


    @RequestMapping("/v_list.do")
    public String list(HttpServletRequest request, ModelMap modelMap){
        return "admin/role/role";
    }

    /**
     * 角色列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryAll")
    @RequiresPermissions("sys:role:list")
    public Object queryAll(@RequestParam Map<String, Object> params, Role role){
        Map<String,Object> map = new HashMap<String, Object>();
        Integer userId =  sessionProvider.getUser().getId();
        //如果不是超级管理员，则只查询自己创建的角色列表
       /* if (roleService.countByUserIdAndName(userId,"'" + Constants.ADMINISTRATOR+ "'")>0) {
            params.put("userId", userId);
        }*/
        PageInfo<Role> pageInfo = roleService.selectAll(params,role);
        map.put("list", pageInfo.getList());
        return map;

    }

    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
    /*@RequiresPermissions("sys:menu:info")*/
    @ResponseBody
    public Object info(@PathVariable("roleId") Integer roleId) {
        Map<String,Object> map = new HashMap<String , Object>();
        Role role = roleService.selectById(roleId);
        map.put("role", role);
        return  map;
    }

    /**
     * 修改、新增操作
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Object saveOrUpdate(HttpServletRequest request, Integer id , @RequestBody Role role, ModelMap modelMap){
        if(role.getId()==null){//新增
            //verifyForm(permission);
            roleService.save(role);
            rolePermissionService.saveOrUpdate(role);
        }else{//修改
            //verifyForm(permission);
            roleService.update(role);
            rolePermissionService.saveOrUpdate(role);
        }
        return new ResultSuccessDto("操作成功");
    }

    /**
     * 删除菜单列表
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(HttpServletRequest request,@RequestBody Integer[] ids){
        if(ids.length==0){
            return new ResultFailDto("参数错误");
        }
        roleService.deleteBatch(ids);

        return new ResultSuccessDto("操作成功");
    }

    /**
     * 角色列表,提供用户选择
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:role:select")
    @ResponseBody
    public Object select() {
        Map<String, Object> map = new HashMap<>();
        Role role = new Role();
        Integer userId = sessionProvider.getUser().getId();
        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if ( !(roleService.countByUserIdAndName(userId,"'" + Constants.ADMINISTRATOR+ "'")>0) ){
            map.put("userId", userId);
        }
        //List<Role> list = (List<Role>) roleService.selectAll(map,role);//强转，不知道会不会出错  
        PageInfo<Role> pageInfo =  roleService.selectAll(map,role);//强转，不知道会不会出错
        map.put("list",pageInfo.getList());
        return map;
    }

    /**
     * 角色列表,会员等级，提供用户选择
     */
    @RequestMapping("/selectLevel")
    /*@RequiresPermissions("sys:role:select")*/
    @ResponseBody
    public Object selectLevel() {
        Map<String, Object> map = new HashMap<>();
        Role role = new Role();
        Integer userId = sessionProvider.getUser().getId();
        if(userId == null){
            return  new RException("账号异常，请重新登录");
        }
        List<Role> list =  roleService.selectLevel();//强转，不知道会不会出错
        map.put("list",list);
        return map;
    }


}
